package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.MedicineDAO;
import cn.edu.scau.cmi.hombio.dao.MedicineStorageDAO;
import cn.edu.scau.cmi.hombio.dao.MedicineStorageDetailDAO;

import cn.edu.scau.cmi.hombio.domain.Medicine;
import cn.edu.scau.cmi.hombio.domain.MedicineStorage;
import cn.edu.scau.cmi.hombio.domain.MedicineStorageDetail;

import cn.edu.scau.cmi.hombio.service.MedicineStorageDetailService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for MedicineStorageDetail entities
 * 
 */

@Controller("MedicineStorageDetailRestController")

public class MedicineStorageDetailRestController {

	/**
	 * DAO injected by Spring that manages Medicine entities
	 * 
	 */
	@Autowired
	private MedicineDAO medicineDAO;

	/**
	 * DAO injected by Spring that manages MedicineStorage entities
	 * 
	 */
	@Autowired
	private MedicineStorageDAO medicineStorageDAO;

	/**
	 * DAO injected by Spring that manages MedicineStorageDetail entities
	 * 
	 */
	@Autowired
	private MedicineStorageDetailDAO medicineStorageDetailDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for MedicineStorageDetail entities
	 * 
	 */
	@Autowired
	private MedicineStorageDetailService medicineStorageDetailService;

	/**
	 * Get Medicine entity by MedicineStorageDetail
	 * 
	 */
	@RequestMapping(value = "/MedicineStorageDetail/{medicinestoragedetail_id}/medicine", method = RequestMethod.GET)
	@ResponseBody
	public Medicine getMedicineStorageDetailMedicine(@PathVariable Integer medicinestoragedetail_id) {
		return medicineStorageDetailDAO.findMedicineStorageDetailByPrimaryKey(medicinestoragedetail_id).getMedicine();
	}

	/**
	* Save an existing MedicineStorageDetail entity
	* 
	*/
	@RequestMapping(value = "/MedicineStorageDetail", method = RequestMethod.PUT)
	@ResponseBody
	public MedicineStorageDetail saveMedicineStorageDetail(@RequestBody MedicineStorageDetail medicinestoragedetail) {
		medicineStorageDetailService.saveMedicineStorageDetail(medicinestoragedetail);
		return medicineStorageDetailDAO.findMedicineStorageDetailByPrimaryKey(medicinestoragedetail.getId());
	}

	/**
	* Save an existing MedicineStorage entity
	* 
	*/
	@RequestMapping(value = "/MedicineStorageDetail/{medicinestoragedetail_id}/medicineStorage", method = RequestMethod.PUT)
	@ResponseBody
	public MedicineStorage saveMedicineStorageDetailMedicineStorage(@PathVariable Integer medicinestoragedetail_id, @RequestBody MedicineStorage medicinestorage) {
		medicineStorageDetailService.saveMedicineStorageDetailMedicineStorage(medicinestoragedetail_id, medicinestorage);
		return medicineStorageDAO.findMedicineStorageByPrimaryKey(medicinestorage.getId());
	}

	/**
	* Register custom, context-specific property editors
	* 
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* Select an existing MedicineStorageDetail entity
	* 
	*/
	@RequestMapping(value = "/MedicineStorageDetail/{medicinestoragedetail_id}", method = RequestMethod.GET)
	@ResponseBody
	public MedicineStorageDetail loadMedicineStorageDetail(@PathVariable Integer medicinestoragedetail_id) {
		return medicineStorageDetailDAO.findMedicineStorageDetailByPrimaryKey(medicinestoragedetail_id);
	}

	/**
	* Create a new MedicineStorageDetail entity
	* 
	*/
	@RequestMapping(value = "/MedicineStorageDetail", method = RequestMethod.POST)
	@ResponseBody
	public MedicineStorageDetail newMedicineStorageDetail(@RequestBody MedicineStorageDetail medicinestoragedetail) {
		medicineStorageDetailService.saveMedicineStorageDetail(medicinestoragedetail);
		return medicineStorageDetailDAO.findMedicineStorageDetailByPrimaryKey(medicinestoragedetail.getId());
	}

	/**
	* Delete an existing MedicineStorage entity
	* 
	*/
	@RequestMapping(value = "/MedicineStorageDetail/{medicinestoragedetail_id}/medicineStorage/{medicinestorage_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteMedicineStorageDetailMedicineStorage(@PathVariable Integer medicinestoragedetail_id, @PathVariable Integer related_medicinestorage_id) {
		medicineStorageDetailService.deleteMedicineStorageDetailMedicineStorage(medicinestoragedetail_id, related_medicinestorage_id);
	}

	/**
	* Delete an existing Medicine entity
	* 
	*/
	@RequestMapping(value = "/MedicineStorageDetail/{medicinestoragedetail_id}/medicine/{medicine_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteMedicineStorageDetailMedicine(@PathVariable Integer medicinestoragedetail_id, @PathVariable Integer related_medicine_id) {
		medicineStorageDetailService.deleteMedicineStorageDetailMedicine(medicinestoragedetail_id, related_medicine_id);
	}

	/**
	* Save an existing Medicine entity
	* 
	*/
	@RequestMapping(value = "/MedicineStorageDetail/{medicinestoragedetail_id}/medicine", method = RequestMethod.PUT)
	@ResponseBody
	public Medicine saveMedicineStorageDetailMedicine(@PathVariable Integer medicinestoragedetail_id, @RequestBody Medicine medicine) {
		medicineStorageDetailService.saveMedicineStorageDetailMedicine(medicinestoragedetail_id, medicine);
		return medicineDAO.findMedicineByPrimaryKey(medicine.getId());
	}

	/**
	* Create a new MedicineStorage entity
	* 
	*/
	@RequestMapping(value = "/MedicineStorageDetail/{medicinestoragedetail_id}/medicineStorage", method = RequestMethod.POST)
	@ResponseBody
	public MedicineStorage newMedicineStorageDetailMedicineStorage(@PathVariable Integer medicinestoragedetail_id, @RequestBody MedicineStorage medicinestorage) {
		medicineStorageDetailService.saveMedicineStorageDetailMedicineStorage(medicinestoragedetail_id, medicinestorage);
		return medicineStorageDAO.findMedicineStorageByPrimaryKey(medicinestorage.getId());
	}

	/**
	* Show all MedicineStorageDetail entities
	* 
	*/
	@RequestMapping(value = "/MedicineStorageDetail", method = RequestMethod.GET)
	@ResponseBody
	public List<MedicineStorageDetail> listMedicineStorageDetails() {
		return new java.util.ArrayList<MedicineStorageDetail>(medicineStorageDetailService.loadMedicineStorageDetails());
	}

	/**
	* View an existing MedicineStorage entity
	* 
	*/
	@RequestMapping(value = "/MedicineStorageDetail/{medicinestoragedetail_id}/medicineStorage/{medicinestorage_id}", method = RequestMethod.GET)
	@ResponseBody
	public MedicineStorage loadMedicineStorageDetailMedicineStorage(@PathVariable Integer medicinestoragedetail_id, @PathVariable Integer related_medicinestorage_id) {
		MedicineStorage medicinestorage = medicineStorageDAO.findMedicineStorageByPrimaryKey(related_medicinestorage_id, -1, -1);

		return medicinestorage;
	}

	/**
	* Create a new Medicine entity
	* 
	*/
	@RequestMapping(value = "/MedicineStorageDetail/{medicinestoragedetail_id}/medicine", method = RequestMethod.POST)
	@ResponseBody
	public Medicine newMedicineStorageDetailMedicine(@PathVariable Integer medicinestoragedetail_id, @RequestBody Medicine medicine) {
		medicineStorageDetailService.saveMedicineStorageDetailMedicine(medicinestoragedetail_id, medicine);
		return medicineDAO.findMedicineByPrimaryKey(medicine.getId());
	}

	/**
	* Get MedicineStorage entity by MedicineStorageDetail
	* 
	*/
	@RequestMapping(value = "/MedicineStorageDetail/{medicinestoragedetail_id}/medicineStorage", method = RequestMethod.GET)
	@ResponseBody
	public MedicineStorage getMedicineStorageDetailMedicineStorage(@PathVariable Integer medicinestoragedetail_id) {
		return medicineStorageDetailDAO.findMedicineStorageDetailByPrimaryKey(medicinestoragedetail_id).getMedicineStorage();
	}

	/**
	* View an existing Medicine entity
	* 
	*/
	@RequestMapping(value = "/MedicineStorageDetail/{medicinestoragedetail_id}/medicine/{medicine_id}", method = RequestMethod.GET)
	@ResponseBody
	public Medicine loadMedicineStorageDetailMedicine(@PathVariable Integer medicinestoragedetail_id, @PathVariable Integer related_medicine_id) {
		Medicine medicine = medicineDAO.findMedicineByPrimaryKey(related_medicine_id, -1, -1);

		return medicine;
	}

	/**
	* Delete an existing MedicineStorageDetail entity
	* 
	*/
	@RequestMapping(value = "/MedicineStorageDetail/{medicinestoragedetail_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteMedicineStorageDetail(@PathVariable Integer medicinestoragedetail_id) {
		MedicineStorageDetail medicinestoragedetail = medicineStorageDetailDAO.findMedicineStorageDetailByPrimaryKey(medicinestoragedetail_id);
		medicineStorageDetailService.deleteMedicineStorageDetail(medicinestoragedetail);
	}
}